/***********************************************
 **** 版权声明处 **
 ****  为了方便阅读和维护，请严格遵守相关代码规范，谢谢   ****
 *******************************************/
package com.hnisi
{
	import com.hnisi.skins.RenderAdonerSkin;
	
	import flash.display.DisplayObject;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	
	import mx.core.FlexGlobals;

	/*******************************************
	 **** @author huangjixin,@date 2012-7-4, @time 下午4:55:40 **
	 **** 请一句话表述该类主要作用  **
	 *******************************************/
	public class RenderAdoner extends Adorner
	{
		//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
		// private 类私有静态变量和静态常量声明处。（全部大写，使用下划线进行分割）
		// 例如：private static const EXAMPLE:String = "example";
		//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
		
		//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
		// public 类公有静态变量和静态常量声明处。（全部大写，使用下划线进行分割）
		// 例如：public static const EXAMPLE:String = "example";
		//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
		
		
		//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
		// private 私有变量声明处，请以“_”开头定义变量
		// 例如：private var _example:String;
		//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
		private var _rect:Rectangle = new Rectangle();
		private var _startPoint:Point = new Point();
		
		private var _pressHandle:Handle;
		//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
		// public 公有变量声明处
		//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
		[SkinPart]
		public var bottomLeftHandle:Handle;
		[SkinPart]
		public var bottomRightHandle:Handle;
		[SkinPart]
		public var topLeftHandle:Handle;
		[SkinPart]
		public var topRightHandle:Handle;
		
		//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
		// 构造函数，初始化相关工作可以放在里面
		//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
		public function RenderAdoner(adornedObject:Node = null)
		{
			setStyle("skinClass", RenderAdonerSkin);
			
			super(adornedObject);
			super.tolerance = 0;
		}//构造函数结束
		
		
		//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
		// getter和setter函数
		//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
		
		
		//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
		// 相关事件响应函数和逻辑函数存放处
		//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
		protected function onHandleMouseDown(event:MouseEvent):void
		{
			handlePressed(event.currentTarget as DisplayObject, event);
		}
		
		protected function onHandleMouseMove(event:MouseEvent):void
		{
			var xPlus:Number = FlexGlobals.topLevelApplication.parent.mouseX - this._startPoint.x;            
			var yPlus:Number = FlexGlobals.topLevelApplication.parent.mouseY - this._startPoint.y;
			
			if(_pressHandle == bottomRightHandle){
				adornedObject.width  = this._rect.width + xPlus < _pressHandle.width * 2 ? _pressHandle.width * 2 : this._rect.width + xPlus;
				adornedObject.height = this._rect.height+ yPlus < _pressHandle.height * 2 ? _pressHandle.height * 2 : this._rect.height + yPlus;
			}else if(_pressHandle == topLeftHandle){
				/*adornedObject.x -= xPlus;
				adornedObject.y -= yPlus;
				adornedObject.width  = this._rect.width + xPlus;
				adornedObject.height = this._rect.height+ yPlus;*/
			}
			
		}
		
		protected function onHandlerMouseUp(event:Event):void
		{
			_pressHandle = null;
			FlexGlobals.topLevelApplication.parent.removeEventListener(MouseEvent.MOUSE_MOVE,onHandleMouseMove);
			FlexGlobals.topLevelApplication.parent.removeEventListener(MouseEvent.MOUSE_UP,onHandlerMouseUp);
		}
		//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
		// override 覆盖函数
		//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
		override protected function partRemoved(partName:String, instance:Object):void{
			super.partRemoved(partName,instance);
			
			if(instance == bottomLeftHandle){
				bottomLeftHandle.removeEventListener(MouseEvent.MOUSE_DOWN,onHandleMouseDown);
			}
			if(instance == bottomRightHandle){
				bottomRightHandle.removeEventListener(MouseEvent.MOUSE_DOWN,onHandleMouseDown);
			}
			if(instance == topLeftHandle){
				topLeftHandle.removeEventListener(MouseEvent.MOUSE_DOWN,onHandleMouseDown);
			}
			if(instance == topRightHandle){
				topRightHandle.removeEventListener(MouseEvent.MOUSE_DOWN,onHandleMouseDown);
			}
		}
		
		override protected function partAdded(partName:String, instance:Object):void{
			super.partAdded(partName,instance);
			
			if(instance == bottomLeftHandle){
				bottomLeftHandle.addEventListener(MouseEvent.MOUSE_DOWN,onHandleMouseDown);
			}
			if(instance == bottomRightHandle){
				bottomRightHandle.addEventListener(MouseEvent.MOUSE_DOWN,onHandleMouseDown);
			}
			if(instance == topLeftHandle){
				topLeftHandle.addEventListener(MouseEvent.MOUSE_DOWN,onHandleMouseDown);
			}
			if(instance == topRightHandle){
				topRightHandle.addEventListener(MouseEvent.MOUSE_DOWN,onHandleMouseDown);
			}
		}
		
		override protected function handlePressed(handle:DisplayObject, event:MouseEvent):void{
			_pressHandle = handle as Handle;
			this._startPoint.x = mouseX;
			this._startPoint.y = mouseY;
			this._startPoint  = this.localToGlobal(_startPoint);
			
			_rect.x = this._startPoint.x;
			_rect.y = this._startPoint.y;
			_rect.width = adornedObject.width;
			_rect.height = adornedObject.height;
			
			FlexGlobals.topLevelApplication.parent.addEventListener(MouseEvent.MOUSE_MOVE,onHandleMouseMove);
			FlexGlobals.topLevelApplication.parent.addEventListener(MouseEvent.MOUSE_UP,onHandlerMouseUp);
		}
		
		override public function isMouseNear(tolerance:Number):Boolean{
			if(this.adornedObject.mouseX > -tolerance && this.adornedObject.mouseX < this.adornedObject.width + tolerance &&
				this.adornedObject.mouseY > -tolerance && this.adornedObject.mouseY < this.adornedObject.height + tolerance){
				return true;
			}
			return false;
		}
	}//类结束
}//包结束